namespace ACEMPortal.UserControls
{
	using System;
	using System.Data;
	using System.Drawing;
	using System.Web;
	using System.Web.UI.WebControls;
	using System.Web.UI.HtmlControls;

	using System.Configuration;
	using ScenarioFramework.WorkflowEngine.DataModel.WorkflowProcesses;
    using ScenarioFramework.Modules.DM.DataModel;
    using ScenarioFramework.Modules.DM.Application;

	/// <summary>
	///		Summary description for DocumentHistory.
	/// </summary>
	public partial class DocumentHistory : System.Web.UI.UserControl
	{

		public string sIdDocument = string.Empty;
		XMLDocumentInstance document = null;

		protected void Page_Load(object sender, System.EventArgs e)
		{
			if ( Page.IsPostBack )
			{
				sIdDocument = (string)ViewState["DocumentId"];
			}
			else
			{
				sIdDocument = Request.QueryString["DocumentId"];
				ViewState["DocumentId"] = sIdDocument;

				document = (XMLDocumentInstance) Session["DocumentInstance"];
                if (document == null)
                    return;
				this.RetrieveVersions();

			}

			
		}

		private void RetrieveVersions()
		{
			try
			{
				DocumentManagerHelper DocHelper = new DocumentManagerHelper();


				DataTable dt = new DataTable();
				dt.Columns.Add(new DataColumn("Revisione", typeof(string)));
				dt.Columns.Add(new DataColumn("Modifica", typeof(string)));
				dt.Columns.Add(new DataColumn("Data Approvazione", typeof(string)));
				dt.Columns.Add(new DataColumn("Visualizza", typeof(string)));
			
				DataRow dr = null;
				XMLDocumentVersion xmlDocumentVersion = null;

				System.Collections.IEnumerator myEnumerator = document.XMLDocumentVersions.GetEnumerator();
				while ( myEnumerator.MoveNext() )
				{
					xmlDocumentVersion = (XMLDocumentVersion)myEnumerator.Current;

					dr = dt.NewRow();

					//retrieve Role from DocumentTemplate inside RELEASELIST Grant type.
					dr[0] = xmlDocumentVersion.Id;
					dr[1] = xmlDocumentVersion.Description;
					dr[2] = xmlDocumentVersion.CreationDate;

					string sNodeName = document.Name.Replace (" ", "_");
					string html = "<a href='DocumentHistory.aspx?DocumentId=" + document.Id + "&DocumentVersion=" + xmlDocumentVersion.Id + "&sm=DocumentationRepository" + "" + "&treenode=" + sNodeName + "'><img height='15' width='15' src='/SFPortal/images/file.gif'></a>";
					dr[3] = html;

					dt.Rows.Add(dr);
				}

				this.dgReleaseList.DataSource = new DataView(dt);
				dgReleaseList.DataBind();
			}
			catch
			{
				// redirect su pagina di errore Parametri di Activity non presenti
				Response.Redirect( SFPortal.Redirect.GoError("NON E' POSSIBILE LEGGERE I DATI XML"));
			}
		}

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		///		Required method for Designer support - do not modify
		///		the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{

		}
		#endregion
	}
}
